#!/bin/bash

yum install -y gcc gcc-c++ yum-utils device-mapper-persistent-data lvm2 zip unzip tree wget curl tree
#卸载旧版本：旧版本的 Docker 被称为docker或docker-engine. 如果安装了这些，请卸载它们以及相关的依赖项
yum remove docker docker-client docker-client-latest docker-common docker-latest docker-latest-logrotate docker-logrotate docker-engine
yum install -y yum-utils device-mapper-persistent-data lvm2
yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
yum makecache fast
# yum install -y docker-ce
# yum list docker-ce --showduplicates | sort -r
yum install -y docker-ce docker-ce-cli containerd.io docker-compose-plugin
# 指定版本安装  yum list containerd.io --showduplicates
# yum install docker-ce-<VERSION_STRING> docker-ce-cli-<VERSION_STRING> containerd.io docker-compose-plugin
# 卸载docker引擎
#yum remove -y docker-ce docker-ce-cli containerd.io docker-compose-plugin
#yum install -y --setopt=obsoletes=0 docker-ce-19.03.15 docker-ce-cli-19.03.15 containerd.io-1.4.3 docker-compose-plugin
#yum install -y --setopt=obsoletes=0 docker-ce-20.10.20 docker-ce-cli-20.10.20 containerd.io-1.4.3 docker-compose-plugin
#yum install -y --setopt=obsoletes=0 docker-ce-20.10.20 docker-ce-cli-20.10.20 containerd.io docker-buildx-plugin docker-compose-plugin


systemctl enable docker && systemctl start docker && docker version

# live-restore 默认情况下，当 Docker 守护进程终止时，它将关闭正在运行的容器。
#     您可以配置守护程序，以便容器在守护程序不可用时保持运行。此功能称为live-restore。
#     live-restore选项有助于减少由于守护进程崩溃、计划中断或升级而导致的容器停机时间
# insecure-registries 仓库地址
# data-root docker镜像的存储目录
cat > /etc/docker/daemon.json << EOF
{
  "registry-mirrors": ["https://fu1rwm7j.mirror.aliyuncs.com"],
  "log-driver": "json-file" ,
  "log-level": "info" ,
  "exec-opts": [ "native.cgroupdriver=systemd" ] ,
  "insecure-registries": [ ] ,
  "live-restore": true
}
EOF

systemctl daemon-reload && systemctl restart docker



echo '开始安装 docker-compose'
#curl -L "https://github.com/docker/compose/releases/download/v2.2.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
#curl -L "https://github.com/docker/compose/releases/download/v2.28.1/docker-compose-linux-x86_64" -o /usr/local/bin/docker-compose
# 查看最新的一个版本
# curl -s https://api.github.com/repos/docker/compose/releases/latest | jq -r .assets[].browser_download_url
latest_download_url=$(curl -s https://api.github.com/repos/docker/compose/releases/latest | jq -r .assets[].browser_download_url | grep docker-compose-linux-x86_64$)
echo "开始下载：$latest_download_url"
curl -L "$latest_download_url" -o /usr/local/bin/docker-compose
chmod +x /usr/local/bin/docker-compose
docker-compose -v
